﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups.Posts
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.enums;
    using SharingCenter.Web.Classes.Groups;
    using System.Xml.Linq;
    using SharingCenter.Web.Classes.Messages;

    public partial class Posts : GroupsPageBase
    {
        protected Sc_Post mainPost;
        Guid _tid;

        protected void Page_Init(object sender, EventArgs e)
        {
            ParseGroupIdFromQueryString();
            HasRights(groupId, Profile.UserId);
            try
            {
                _tid = new Guid(Request.QueryString["t"]);
            }
            catch { RedirectToDefaultPage(); }

            if (Request.QueryString["u"] != null)
            {
                // update view count
                PostOperations.UpdateViewCount(_tid);
                RedirectToPostPage(_tid, false);
                Response.Flush();
            }

            FormView1.Visible = member != null;
            msgMemberOnlyForReply.Visible = member == null;

            mainPost = PostOperations.GetMainPostInThread(_tid);
            lvPosts.ItemCommand += new EventHandler<ListViewCommandEventArgs>(lvPosts_ItemCommand);
            FormView1.ItemInserting += new FormViewInsertEventHandler(FormView1_ItemInserting);
            FormView1.ItemInserted += new FormViewInsertedEventHandler(FormView1_ItemInserted);
            
            //LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
        }

        private SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
            SiteMapNode tmpNode = currentNode;
            tmpNode.Title = mainPost.PostTitle;
            if (null != (tmpNode = tmpNode.ParentNode))
            {
                tmpNode.Url += "?id=" + groupId;
            }

            if (null != (tmpNode = tmpNode.ParentNode))
            {
                tmpNode.Title = group.GroupName;
                tmpNode.Url += "?id=" + groupId;
            }
            return currentNode;
        }

        

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {/*
            using (scDataContext cxt = new scDataContext())
            {
                e.Result = cxt.GetPostsByThreadId(_tid).ToList();
            }*/
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
            BindThreadInfo();
        }

        protected void Page_Unload(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(this.SiteMap_SiteMapResolve);
        }

        void BindThreadInfo()
        {
            Sc_PostThread thread = PostOperations.GetThreadByThreadId(_tid);
            ltrTitle.Text = mainPost.PostTitle;
            ltrMainPostTitle.Text = mainPost.PostTitle;
            ltrMainPostTitle.ToolTip = mainPost.PostTitle;
            lblViewCount.Text = thread.ViewCount.ToString();
            lblPostCount.Text = thread.TotalPosts.ToString();
        }

        void lvPosts_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "DeletePost":
                    PostOperations.MarkPostAsDeleted(new Guid(e.CommandArgument.ToString()));
                    break;
                default: break;
            }

            RefreshPage();
        }

        void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            e.Values["PostId"] = Guid.NewGuid();
            e.Values["PostDate"] = DateTime.Now;
            e.Values["LastUpdateDate"] = DateTime.Now;
            e.Values["PostType"] = PostType.Original;
        }

        void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception == null)
            {
                SendReplySystemMessage();
                RefreshPage(true);
            }
        }

        void SendReplySystemMessage()
        {
            if (mainPost.PostUserId != Profile.UserId)
            {
                Sc_Group group = GroupOperations.GetGroupByGroupId(groupId);
                string eventName = "GroupPostReply";
                // Send system message
                XElement context = new XElement("Context",
                    new XAttribute("XsltFolderInMod", "Groups"),
                    new XElement("Event",
                        new XAttribute("Name", eventName),
                        new XElement("PostTitle", mainPost.PostTitle),
                        new XElement("GroupId", group.GroupId),
                        new XElement("GroupName", group.GroupName),
                        new XElement("ReplierUserId", Profile.UserId),
                        new XElement("ThreadId", _tid)));
                MessageOperations.SendSystemMessage(Profile.UserId, (Guid)mainPost.PostUserId, eventName, context, true);
            }
        }

        protected void txtReplyTitle_Load(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = string.Format("回复: {0}", mainPost.PostTitle);
        }

        protected void mvReply_PreRender(object sender, EventArgs e)
        {
            ((MultiView)sender).ActiveViewIndex = member == null ? 1 : 0;
        }
    }
}
